<?php

namespace app\admin\controller\wwh;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 栏目管理
 */
class Column extends Backend
{
    protected $columnList = [];
    protected $multiFields = ['weigh', 'isnav'];
    protected $searchFields = 'name';

    /**
     * Column模型对象
     * @var \app\admin\model\wwh\column
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wwh\Column;
        $this->tree = Tree::instance();
        $this->tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'parent_id');
        $this->columnList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
        $this->view->assign("columnList", $this->columnList);
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("classifyList", $this->model->getClassifyList());
        $this->assignconfig("classifyList", $this->model->getClassifyList());
        $this->assignconfig("tplList", $this->model->getTplList());
        $this->view->assign("tplList", $this->model->getTplList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $search = $this->request->request("search");
            //构造父类select列表选项数据
            $list = [];
            if ($search) {
                foreach ($this->columnList as $k => $v) {
                    if (stripos($v['name'], $search) !== false) {
                        $list[] = $v;
                    }
                }
            } else {
                $list = $this->columnList;
            }
            $total = count($list);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

}
